import request from '@/utils/request'
import { mockInventoryList } from '@/api/inventory'

let mockProductList = [
  { id: 1, name: '商品A', code: 'A001', category: '食品', price: 10.5, stock: 100, status: '上架' },
  { id: 2, name: '商品B', code: 'B002', category: '饮料', price: 3.0, stock: 50, status: '上架' }
];

export function getProductList() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockProductList.length, items: mockProductList }
      });
    }, 300);
  });
}

export function getProductDetail(id) {
  return request({
    url: `/products/${id}`,
    method: 'get'
  })
}

export function addProduct(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const newItem = { ...data };
      mockProductList.unshift(newItem);
      mockInventoryList.unshift({
        id: newItem.id,
        name: newItem.name,
        code: newItem.code,
        category: newItem.category,
        stock: 0,
        stockStatus: 'critical'
      });
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function updateProduct(id, data) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockProductList = mockProductList.map(item => item.id === id ? { ...item, ...data } : item);
      resolve({ code: 20000, data: { ...data, id } });
    }, 300);
  });
}

export function deleteProduct(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockProductList = mockProductList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
}

export function getProductCategories() {
  // return request({
  //   url: '/products/categories',
  //   method: 'get'
  // })

  // 模拟数据，用于前端开发调试
  const mockCategories = [
    { id: 1, name: '食品' },
    { id: 2, name: '饮料' },
    { id: 3, name: '日用品' },
    { id: 4, name: '电子产品' },
    { id: 5, name: '其他' }
  ]

  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: mockCategories
      })
    }, 300) // 模拟网络延迟
  })
}

export function addProductCategory(data) {
  return request({
    url: '/products/categories',
    method: 'post',
    data
  })
}

export function updateProductCategory(id, data) {
  return request({
    url: `/products/categories/${id}`,
    method: 'put',
    data
  })
}

export function deleteProductCategory(id) {
  return request({
    url: `/products/categories/${id}`,
    method: 'delete'
  })
} 